<?php
/**
 * This class provides varias utility functions for debugging the application.
 *
 * @author Ishtiaque Shahrier <md.ishtiaque@newgenbd.net>
 * @package core
 * @subpackage libraries
 */
class console extends a_front_controller {
    /**
     *
     * @var string all msgs are stacked in here
     */
    private $msg;
    /**
     *
     * @var array an array of basic information
     */
    private $info = array();

    public function __construct() {
        parent::__construct();
        $this->info['MYSQL Vresion'] = get_instance()->db->query("SELECT version() as version")->row()->version;
        $this->info['Document Root'] = $_SERVER['DOCUMENT_ROOT'];
        $this->info['Server Software'] = $_SERVER['SERVER_SOFTWARE'];
        $this->info['Your IP Address'] = $this->input->ip_address();
        $this->info['Server IP Address'] = $_SERVER['SERVER_ADDR'];
    }
    /**
     * Returns TRUE is the current ip address is added  into the debug_ips table
     * @return bool
     */
    private function _has_permission() {
        return has_debug_permission();
    }
    /**
     * @internal
     */
    public function _remap($method) {
        if ($method !== "enable") {
            if (!$this->_has_permission()) {
                show_error("Your ip is not allowed");
            }
        }
        parent::_remap($method);
    }
    /**
     * @internal
     */
    public function do_partial_rendering() {
        return TRUE;
    }
    /**
     * @internal
     */
    public function batch_assign() {
        $this->assign("msg", $this->msg);
        $this->assign("info", $this->info);
    }
    /**
     * Shows information about the debugging ability your IP address
     * @return void
     */
    public function debug_ip_info() {
        $obj = new debug_ip_m_core();
        if (!$obj->valid_debug_ip($this->input->ip_address())) {
            $this->msg = "debug_ip information not avaiable";
        } else {
            $ip = $this->input->ip_address();
            ob_start();
            debug_h("Printing debug_ip information for  $ip", 4);
            echo "<ul>";
            $debug = $obj->is_debug_print_on($ip) ? "on" : "off";
            $smarty = $obj->is_smarty_console_on($ip) ? "on" : "off";
            $maintenance = $obj->is_ignore_maintenance($ip) ? "on" : "off";
            echo "<li><strong>Debug Print</strong> : " . $debug . "</li>";
            echo "<li><strong>Smarty Console</strong> : " . $smarty . "</li>";
            echo "<li><strong>Ignore Maintenance</strong> : " . $maintenance . "</li>";
            echo "</ul>";
            $this->msg = ob_get_contents();
            ob_end_clean();
        }
    }
    /**
     * Sets ignoring maintenance
     * @param 1|0 $flag
     * @return void
     */
    public function set_ignore_maintenance($flag=1) {
        $obj = new debug_ip_m_core();
        if ($obj->valid_debug_ip($this->input->ip_address())) {
            $obj->set_ignore_maintenance($this->input->ip_address(), $flag);
            $this->msg = "Maintenanc is set to $flag";
        } else {
            $this->msg = "Add IP to debug_ips first";
        }
    }
    /**
     * Sets the visibility inline debugging
     * @param 1|0 $flag
     * @return void
     */
    public function set_debug_print($flag=1) {
        $obj = new debug_ip_m_core();
        if ($obj->valid_debug_ip($this->input->ip_address())) {
            $obj->set_debug_print($this->input->ip_address(), $flag);
            $this->msg = "Debug print is set to $flag";
        } else {
            $this->msg = "Add IP to debug_ips first";
        }
    }
    /**
     * Sets the smarty console show/hide
     * @param 1|0 $flag
     * @return void
     */
    public function set_smarty_console($flag=1) {
        $obj = new debug_ip_m_core();
        if ($obj->valid_debug_ip($this->input->ip_address())) {
            $obj->set_smarty_console($this->input->ip_address(), $flag);
            $this->msg = "Smarty debug console is set to $flag";
        } else {
            $this->msg = "Add IP to debug_ips first";
        }
    }
    /**
     * Removes your IP address from the debug_ips table     
     * @return void
     */
    public function remove_debug_ip() {
        $obj = new debug_ip_m_core();
        $obj->remove_debug_ip($this->input->ip_address());
        $this->msg = "Your IP is removed";
    }
    /**
     * Adds your ip address into the debug_ips table
     */
    public function add_debug_ip() {
        $obj = new debug_ip_m_core();
        $obj->insert_debug_ip($this->input->ip_address());
        $this->msg = "Your IP is added to debug_ips ";
    }
    /**
     * Sets session variable with gicven name and value
     * @param string $var
     * @param string $value
     */
    public function set_session($var, $value) {
        $this->session->set_userdata($var, $value);
        $this->msg = 'Successfully Set $_SESSION[\'' . $var . '\'] = ' . $value;
    }
    /**
     * Unsets the given variable from the session.
     * If nothing is suuplied as the variable name then the entire session is destroyed
     * @param string $var default is NULL
     */
    public function unset_session($var=NULL) {
        if ($var == NULL) {
            @session_destroy();
            $this->msg = "Entire session has been destroyed";
        } else {

                unset($_SESSION[$var]);
                $this->msg = "Unset the session variable <strong>$var</strong>";

        }
    }
    /**
     * Prints the specific session variable if supplied else the entire session is printed
     * @param string $var  edfault is NULL
     * @return void
     */
    public function print_session($var=NULL) {
        ob_start();

        if ($var !== NULL) {
            debug_h("Printing " . '$_SESSION[\'' . $var . '\']', 3);
            echo "<strong>";
            var_dump($this->session->userdata($var));
            echo "</strong> ";
        } else {
            debug_h("Printing entire session ", 3);
            echo "<ul>";
            foreach ($_SESSION as $key => $val) {
                echo "<li><strong>$key</strong> : " . (is_array($val) ? print_r($val, TRUE) : $val) . "</li>";
            }
            echo "</ul>";
        }

        $this->msg = ob_get_contents();
        ob_end_clean();
    }
    /**
     * Wrapper for the phpinfo() built in function.
     * @return void
     */
    public function php_info() {
        ob_start();
        phpinfo();
        $this->msg = ob_get_contents();
        ob_end_clean();
    }
    /**
     * Prints the app db config file which is actually this file APPPATH/config/database.php file
     * @return void
     */
    public function db_config() {
        include APPPATH . "/config/database.php";
        ob_start();
        debug_h("Printing database.php ", 3);
        foreach ($db as $key => $val) {
            debug_h("Printing $key database config ", 4);
            echo "<ul>";
            foreach ($val as $key2 => $val2) {
                echo "<li><strong>$key2</strong> : $val2</li>";
            }
            echo "</ul>";
        }


        $this->msg = ob_get_contents();
        ob_end_clean();
    }
    /**
     * Prints the app config which is actually this file APPPATH/config/config.php file
     * @return void
     */
    public function app_config() {
        include APPPATH . "/config/config.php";
        ob_start();
        debug_h("Printing config.php ", 3);
        echo "<ul>";
        foreach ($config as $key => $val) {
            echo "<li><strong>$key</strong> : " . ($val === TRUE ? "TRUE" : ($val === FALSE ? "FALSE" : $val)) . " </li>";
        }
        echo "</ul>";

        $this->msg = ob_get_contents();
        ob_end_clean();
    }
    /**
     * @internal
     */
    function index() {
        $this->msg = "welcome to ngbdcart debug console!!";
    }
    /**
     * Enables the console service for your IP if the given password is correct
     * @return void
     */
    public function enable($password=NULL) {
        if (!$this->_has_permission()) {
            if (strlen($password)) {
                $pw = service_l_core::get_global_setting('debug_console_password', TRUE);
                if (md5($password) === $pw) {
                    $existing_ips = (string) service_l_core::get_global_setting('debug_console_ips', TRUE);
                    get_instance()
                            ->db->set('value', $existing_ips . ";" . $this->input->ip_address())
                            ->where("setting_name", "debug_console_ips")
                            ->update('global_settings');
                    redirect("core/console/index");
                    ;
                } else {
                    $this->msg = "Debug console passowrd is not valid!";
                }
            } else {
                $this->msg = "Debug console passowrd is not valid!";
            }
        } else {
            redirect("core/console/index", 'refresh');
        }
    }
    /**
     * Disables concole service for YOUR ip
     * @return void
     */
    public function disable() {
        $ips = explode(";", (string) service_l_core::get_global_setting('debug_console_ips', TRUE));
        foreach ($ips as &$ip) {
            if ($ip === $this->input->ip_address()) {
                $ip = NULL;
                get_instance()
                        ->db->set('value', implode(";", $ips))
                        ->where("setting_name", "debug_console_ips")
                        ->update('global_settings');
            }
        }
        redirect("core/console/index", 'refresh');
    }

    /**
     * Show loaded module list
     * @return void
     */
    public function module_list(){
        ob_start();
        debug_h("Printing loaded modules ", 3);
        $modules = Module_Extended::list_active_modules();
        echo "<ul>";
        foreach ($modules as $module) {
           $name = $module->name;
           $desc = $module->description;
           $version = $module->version;
           $priority = $module->load_priority;
           echo "<li><strong>$name</strong> <span style='color:blue'> V:$version</span> <span style='color:green'>P:$priority </span>  <span style='color:gray'>[ $desc ]</span></li>";
        }
        echo "</ul>";

        $this->msg = ob_get_contents();
        ob_end_clean();
    }


}
?>